package cn.miaopasi.boot.clickhouse.controller;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.ObjectId;
import cn.miaopasi.boot.clickhouse.model.AppInfo;
import cn.miaopasi.boot.clickhouse.service.IAppInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;


/**
 * AppInfo控制层
 *
 * @author lixin
 */
@RestController
@RequestMapping("/app")
public class AppInfoCtl {

    @Resource
    private IAppInfoService appInfoService;

    /**
     * 创建应用
     *
     * @param appName 应用名称
     */
    @PostMapping("create")
    public SaResult create(@RequestParam("appName") String appName) {
        AppInfo record = AppInfo.builder()
                .id(ObjectId.next())
                .appId(ObjectId.next())
                .appName(appName)
                .secretId(ObjectId.next())
                .secretKey(ObjectId.next())
                .status(0)
                .gmtCreate(LocalDateTime.now())
                .build();
        appInfoService.save(record);
        // 返回应用信息
        Dict dict = Dict.create()
                .set("appId", record.getAppId())
                .set("secretId", record.getSecretId())
                .set("secretKey", record.getSecretKey());
        return SaResult.data(dict);
    }

    /**
     * 查询记录信息
     *
     * @param id 记录ID
     */
    @GetMapping("get/{id}")
    public SaResult get(@PathVariable(name = "id") String id) {
        AppInfo record = appInfoService.getById(id);
        return SaResult.data(record);
    }

}
